import {
    post
} from "../../utils/http"

// pages/waterfall/waterfall.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        leftList: [],
        rightList: [],
        allList: [],
        leftHeight: 0,
        rightHeight: 0,
        flag: false,
        total: 0,
        isShow: false
    },
    pageSize: 10,
    pageNum: 1,


    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.initData()
    },
    initData() {
        this.data.flag = false
        this.setData({
            flag: false
        })
        console.log(this.data.flag);
        console.log(this.pageNum);
        post('/api/bbs/bbsPosts/open/list', {
            pageSize: this.pageSize,
            pageNum: this.pageNum
        }).then(res => {
            console.log(res.data.total);
            this.data.allList = this.data.allList.concat(res.data.rows)
            this.setData({
                allList: this.data.allList,
                total: res.data.total
            })

            console.log(this.data.allList);
            this.handleList()

        })
    },
    handleList() {
        if (this.data.allList.length == 0) {

            if (this.pageNum * this.pageSize >= this.data.total) {
                this.setData({
                    flag: false,
                    isShow: true
                })
                console.log(this.data.isShow);
                return
            } else {
                this.setData({
                    flag: true
                })
                return
            }

        }
        const item = this.data.allList.shift();
        wx.getImageInfo({
            src: item.coverImgUrl,
            success: (res) => {
                if (this.data.leftHeight <= this.data.rightHeight) {
                    this.data.leftList.push(item)
                    this.data.leftHeight += res.height / res.width
                } else {
                    this.data.rightList.push(item)
                    this.data.rightHeight += res.height / res.width
                }
                this.setData({
                    leftList: this.data.leftList,
                    rightList: this.data.rightList
                })
                //   this.flag = true

            },
            fail: (err) => {
                console.log(err);
            },
            complete: () => {
                this.handleList()
            }

        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        console.log(11111);
        if (this.data.flag) {
            this.pageNum = 1
            this.setData({
                leftList: [],
                rightList: [],
            })
            this.initData();
        }

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if (this.data.flag) {
            this.pageNum++;
            console.log(this.data.flag);
            this.initData()
        }

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    skipChat(e) {
        if (getApp().globalData.isLogin) {
            wx.navigateTo({
                url: '/pages/chat/chat?id=' + e.currentTarget.dataset.id,
            })
        } else {
            wx.switchTab({
                url: '/pages/mine/mine',
            })
        };

    },
    // 获取滚动条当前位置
    onPageScroll: function (e) {
        console.log(e)
        if (e.scrollTop > 100) {
            this.setData({
                floorstatus: true
            });
        } else {
            this.setData({
                floorstatus: false
            });
        }
    },

    //回到顶部
    goTop: function (e) { // 一键回到顶部
        if (wx.pageScrollTo) {
            wx.pageScrollTo({
                scrollTop: 0
            })
        } else {
            wx.showModal({
                title: '提示',
                content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
            })
        }
    },
})